﻿using System;
using System.Linq;
using System.Web.Mvc;
using Icms.Core.Model;
using Icms.Core.Repository;

namespace Icms.WebUI.Controllers
{
    public class CalendarController : BaseController
    {
        private readonly IRepo<Hearing> r;

        public CalendarController(IRepo<Hearing> r)
        {
            this.r = r;
        }

        public JsonResult GetEvents(double start, double end, Guid CaseId, Guid? RoomId)
        {
            var fromDate = ConvertFromUnixTimestamp(start);
            var toDate = ConvertFromUnixTimestamp(end);
            var res = r.Where(o => o.CaseId == CaseId && o.Date > fromDate && o.Date < toDate);
            if (RoomId.HasValue) res = res.Where(o => o.RoomId == RoomId);

            var es = res
                .Select(o =>
                new { id = o.Id, title = "titlu", 
                    start = o.Date.ToString("s"), 
                    end = o.DateEnd.ToString("s"), 
                    allDay = false });
           
            return Json(es, JsonRequestBehavior.AllowGet);

        }

        private static DateTime ConvertFromUnixTimestamp(double timestamp)
        {
            var origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return origin.AddSeconds(timestamp);
        }
    }
}